package com.postracker.postracker.dao.util;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

/**
 * This class gets the configuration from the file hibernate.cfg.xml
 * 
 * @author ademiguel
 * 
 */
public class HibernateConnection {

	private static final HibernateConnection INSTANCE = new HibernateConnection();

	private static SessionFactory sessionFactory;
	private static ServiceRegistry serviceRegistry;

	private HibernateConnection() {
		// Limit the scope;
	}

	public static HibernateConnection getInstance() {
		return INSTANCE;
	}

	public SessionFactory getSessionFactory() {
		Configuration configuration = new Configuration();
		configuration.configure();
		serviceRegistry = new ServiceRegistryBuilder().applySettings(
				configuration.getProperties()).buildServiceRegistry();
		sessionFactory = configuration.buildSessionFactory(serviceRegistry);
		return sessionFactory;
	}

	public void shutdown() {
		getSessionFactory().close();
	}
}
